<?php
namespace api\jeu\perso\buff;

class Buff {

	private $_value;
	private $_source;
	
	private $_valuePercent;
	private $_valueAbsolute;
	
	private $_stack;
	
	private $_multiplier;
	
	public $rebuffs = null;
	
	public $isDead;
	public $rebuff;
	
	private $_caracteristiques = array();
	
	public function __construct($value, $source = null, $valuePercent = false, $valueAbsolute = false, $stack = 1, $multiplier = 1, $rebuffs = null)
	{
		$this->_value = $value;
		$this->_source = $source;
		$this->_valuePercent = $valuePercent;
		$this->_valueAbsolute = $valueAbsolute;
		$this->_stack = $stack;
		$this->_multiplier = $multiplier;
		
		if(is_array($rebuffs))
		{
			$this->rebuffs	= $rebuffs;
		}
		
		$this->isDead = false;
		$this->rebuff = false;
	}

	public function addCaracteristique($carac)
	{
		$this->_caracteristiques[] = $carac;
	}
	
	public function removeCaracteristique($carac)
	{
		$cle = array_search($carac, $this->_caracteristiques);
		
		if($cle == false)
		{
			unset($this->_caracteristiques[$cle]);
		}
		
		$cle = array_search($carac, $this->_caracteristiques);		
	}	
	
	public function getCaracteristique()
	{
		return $this->_caracteristiques;
	}
	
	public function tick()
	{
		$this->_value *= $this->_multiplier;
		$this->_stack--;
				
		if($this->_stack <= 0)
		{
			if($this->rebuffs)
			{
				$this->rebuff = true;
			} 
			else
			{
				$this->isDead = true;
			}		
		}
	}
	
	public function getStack()
	{
		return $this->_stack;
	}
	
	public function update(Buff $buff)
	{
		$this->_value += $buff->_value;
	}
	
	public function getValue()
	{
		return $this->_value;
	}
	
	public function getSource()
	{
		return $this->_source;
	}
	
	public function setValue($val)
	{
		$this->_value = $val;
		return $this;
	}
	
	public function setSource($val)
	{
		$this->_source = $val;
		return $this;
	}
	
	public function apply($modifier,$source)
	{
		return $this->applyRules($modifier);
	}
	
	public function applyRules($val)
	{
		return $this->_value + $val;
	}	
	
}